var userId=getCookie("userId");
//点击弹出框的取消关闭弹出框
// $("#can").on("click",".cancle , .close",function() {
//     console.log(33321)
//     $(".opacity_bg").hide();
//     $("#can").empty()
//     return false;
// })
$(".close").on("click",function() {
    console.log(33321)
    $(".opacity_bg").hide();
    $("#can").empty()
    return false;
})
$(".cancle").on("click",function() {
    console.log(33321)
    $(".opacity_bg").hide();
    $("#can").empty()
    return false;
})
//创建笔记本
$("#can").off("click").on("click", "#sure",function() {
    var bookName=$("#input_notebook").val().trim();
    if(bookName.length==0){
        alert("笔记本名不能为空")
        return
    }
    $.ajax({
        url: base_url +"/notebook/save.do",
        type: "post",
        data: {
            "userId":userId,
            "noteBookName":bookName
        },
        dataType: "json",
        success:function(data) {
            if (data.status==0) {
                $(".opacity_bg").hide();
                $("#can").empty()
                var bookId=data.data.noteBookId
                var s_li ='<li class="online" >'
                s_li += '<a id="bookName">'
                s_li += '<i class="fa fa-book" title="online" rel="tooltip-bottom">'
                s_li += '</i> '+bookName
                s_li += '</a>'
                s_li += '</li>'
                var $li = $(s_li)//将html字符串转换成jquery对象
                $li.data("bookId",bookId)//将bookId绑定到li元素中
                //将li元素添加到ul
                $("#book_list").append($li)
                console.log($li.data("bookId"))
                return false;
            }else {
                $(".opacity_bg").hide();
                $("#can").empty()
            }
        },
        error : function(data) {
            alert("添加失败,稍后重试")
        }
    })
})
//创建笔记
$("#can").on("click", "#addNote",function() {
    var noteTittle=$("#input_note").val().trim();
    if(noteTittle==""){
        alert("标题不能为空")
        return;
    }
    var $li=$("#book_list li a.checked").parent();
    var noteBookId=$li.data("bookId")
    console.log(noteBookId)
    $.ajax({
        url: base_url +"/note/save.do",
        type: "post",
        data: {
            "userId":userId,
            "noteTittle":noteTittle,
            "noteBookId":noteBookId
        },
        dataType: "json",
        success:function(data) {
            if (data.status==0) {
                $(".opacity_bg").hide();
                $("#can").empty()
                var noteId=data.data.noteId;
                var noteName=data.data.noteTittle;
                var s_li ='<li class="online" >'
                s_li += '<a>'
                s_li += '<i class="fa fa-file-text-o" title="online" rel="tooltip-bottom">'
                s_li += '</i> '+noteName
                s_li += '<button type="button" class="btn btn-default btn-xs btn_position btn_slide_down"><i class="fa fa-chevron-down"></i></button>'
                s_li += '</a>'
                s_li +='<div class="note_menu" tabindex=\'-1\'>'
                s_li += ' <dl>'
                s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_move" title=\'移动至...\'><i class="fa fa-random"></i></button></dt> '
                s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_share" title=\'分享\'><i class="fa fa-sitemap"></i></button></dt> '
                s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_delete" title=\'删除\'><i class="fa fa-times"></i></button></dt> '
                s_li += ' </dl>'
                s_li += ' </div>'
                s_li += '</li>'
                var $li = $(s_li)//将html字符串转换成jquery对象
                $li.data("noteId",noteId)
                //将li元素添加到ul
                $("#note_list").append($li)
                return false;
            }else {
                $(".opacity_bg").hide();
                $("#can").empty()
            }
        },
        error : function(data) {
            alert("添加失败,稍后重试")
        }
    })
})
//移动笔记
$("#can").on("click","#sureMove",function (){
    let $li = $("#note_list a.checked").parent("li");
    let noteId = $li.data("noteId");
    console.log(noteId)
    $("#moveSelect option:selected").each(function() {
        let noteBookId = $(this).val();

        $.ajax({
            url: base_url + "/note/move.do",
            type: "post",
            data: {
                "noteBookId": noteBookId,
                "noteId": noteId
            },
            dataType: "json",
            success:function(data) {
                if (data.status==0) {
                    let msg = data.msg;
                    alert(msg)
                    $li.remove();
                    return false;
                }
            },
            error:function(XMLHttpRequest,textStatus,errorThrown) {
                alert("查询失败,稍后重试")
            }
        })
    })

})

